package com.chx.three.mama.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.model.BeautyDetail;
import com.chx.three.mama.utils.BitmapHelper;
import com.lidroid.xutils.BitmapUtils;

import java.util.LinkedList;

/**
 * Created by aaa on 15-3-29.
 */
public class BeautyDetailAdapter extends BaseAdapter {

    private LinkedList<BeautyDetail> beautyDetails;
    private Context context;

    public BeautyDetailAdapter(LinkedList<BeautyDetail> beautyDetails, Context context) {
        this.beautyDetails = beautyDetails;
        this.context = context;
    }

    @Override
    public int getCount() {
        return beautyDetails.size();
    }

    @Override
    public Object getItem(int position) {
        return beautyDetails.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        int ret = 0;
        BeautyDetail beautyDetail = beautyDetails.get(position);
        int type = beautyDetail.getType();
        if (type==1)
            ret=0;
        else
            ret=1;
        return ret;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        BeautyDetail beautyDetail = beautyDetails.get(position);
        int type = beautyDetail.getType();
        if (convertView!=null){
            ret=convertView;
        }else {
            LayoutInflater inflater = LayoutInflater.from(context);
            if (type==1){
               ret=inflater.inflate(R.layout.item_beauty_detail_content_txt,parent,false);
            }else if (type==2){
                ret=inflater.inflate(R.layout.item_beauty_detail_content_img,parent,false);
            }
        }

        if (ret!=null){
            if (type==1){
                bindTxtView(ret,beautyDetail);
            }else if (type==2){
                bindImgView(ret,beautyDetail);
            }
        }
        return ret;
    }

    private void bindTxtView(View ret,BeautyDetail detail){
        if (ret!=null&&detail!=null){
            TxtHolder holder = (TxtHolder) ret.getTag();
            if (holder==null){
                holder=new TxtHolder();
                holder.txt = (TextView) ret.findViewById(R.id.item_detail_content_txt);
                ret.setTag(holder);
            }
            holder.txt.setText(detail.getContent());
        }
    }

    private void bindImgView(View ret,BeautyDetail detail){
        if (ret!=null&&detail!=null){
            ImgHolder holder = (ImgHolder) ret.getTag();
            if (holder==null){
                holder=new ImgHolder();
                holder.img = (ImageView) ret.findViewById(R.id.item_detail_content_img);
                ret.setTag(holder);
            }
            String content = detail.getContent();
            BitmapUtils bitmapUtils = BitmapHelper.getBitmapUtils();
            bitmapUtils.display(holder.img,content);
        }
    }

    private static class TxtHolder{
        public TextView txt;
    }

    private static class ImgHolder{
        public ImageView img;
    }
}
